﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//using System;

public class LoadScene : MonoBehaviour {

	public static string name = null;
	//异步对象
	AsyncOperation anync;
	//进度值
	int pregress = 0;
	Image image;
	Text text;
    //记载进度
	float m_a=0;
    float m_a2 = 0;
	float[] time = new float[3];
	bool[] m_bool = new bool[3];
	bool begin_bool = true;
	void Start () {
		image = GameObject.Find("/Canvas/Loading/Panel_jindu").gameObject.GetComponent<Image>();
		text = GameObject.Find("/Canvas/Loading/Text").gameObject.GetComponent<Text>();
		StartCoroutine(Load(name));
		anync.allowSceneActivation = false;

		for(int i=0;i<3;i++)
		{
			m_bool[i] = false;
			m_bool[0] = true;
			time[i] =1;
            time[0] = 0.5F;
		}
	}

	void Update () {
	   // pregress = Convert.ToInt32((anync.progress+0.1F)*100);
		if(m_bool[0])
		{
		   time[0]-=Time.deltaTime;
		   if(time[0]<=0)
		   {
			  m_a = Random.Range(30,50);
			  m_bool[1] = true;
			  m_bool[0] = false;
		   }
		}
		if(m_bool[1])
		{
			time[1]-=Time.deltaTime;
			if(time[1]<=0)
			{
				m_a = Random.Range(70,90);
				m_bool[2] = true;
				m_bool[1] = false;
			}
		}
		if(m_bool[2])
		{
			time[2]-=Time.deltaTime;
			if(time[2]<=0)
			{
				m_a = 100;
				m_bool[2] = false;
			}
		}

        m_a2++;
        if (m_a2 >= m_a)
        {
            m_a2 = m_a;
        }
		text.text = m_a2+" %";//pregress.ToString()+"%"; 
		image.fillAmount = m_a2/100;//anync.progress+0.1F;
		if(m_a2==100 && anync.progress>=0.9F)
		{
			anync.allowSceneActivation = true;
		}

	}

	public IEnumerator Load(string str)
	{
		anync = Application.LoadLevelAsync(str);
		yield return anync;
	}

}
